import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;

public class Configuration {

	private static Configuration instance = null;
	private ArrayList<ServerProfile> serverProfiles = new ArrayList<>();
	private int numOfServer;

	private Configuration() {
		Properties prop = new Properties();
		InputStream input = null;

		try {

			input = new FileInputStream("configFile");

			// load a properties file
			prop.load(input);
			
			String[] names = prop.getProperty("servers").split(",");
			numOfServer = names.length;
			for (int i = 0; i < names.length; i++) {
				String name = names[i];
				String[] ad = prop.getProperty(name).split(":");
				serverProfiles.add(new ServerProfile(name, ad[0], ad[1], Arrays.copyOfRange(ad,2,5), numOfServer));
			}
			
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static Configuration getInstance() {
		if (instance == null) {
			instance = new Configuration();
		}
		return instance;
	}
	
	public ArrayList<ServerProfile> getServerProfiles() {
		return serverProfiles;
	}
	
	public int getNumOfServers() {
		return numOfServer;
	}
	
	public ServerProfile getServerByName(String name) {
		for (ServerProfile user : serverProfiles) {
			if (user.getName().equalsIgnoreCase(name)) {
				return user;
			}
		}
		System.err.println("Warning: User " + name + " is null!");
		return null;
	}
	
}